package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.DealDao;
import cn.ym.mall.entity.Deal;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 交易 DA0 实现类
 */
public class DealDaoImpl implements DealDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增交易表
     *
     * @param deal 添加的交易表
     * @return 返回成功的条数
     */
    @Override
    public Integer addDeal(Deal deal) throws SQLException {
        String sql = "insert into ym_deal(uname, sid, gid, cinfoid,ostartTime,oendTime, ocount, oprice) values (?,?,?,?,?,?,?,?);";
        Object[] params = new Object[]{deal.getUname(),deal.getSid(),deal.getGid(),deal.getCinfoid(),deal.getOstartTime(),deal.getOendTime(),deal.getOcount(),deal.getOprice()};
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 根据交易id查询交易信息
     *
     * @param dealid
     * @return
     */
    @Override
    public Deal getDealByStid(Integer dealid) throws SQLException {
        String sql = "select * from ym_deal where dealid = ?;";
        return queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Deal>(Deal.class),dealid);
    }

    /**
     * 查询交易总数
     *
     * @return 交易总数量
     */
    @Override
    public long getDealCount() throws SQLException {
        long num = 0;
        try{
            String sql = "select count(0) as count from ym_deal;";
            num = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<>());
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过商家id统计交易总数
     *
     * @param sid 商店编号
     * @return 返回条数
     */
    @Override
    public long getCountBySid(Integer sid) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_deal where sid = ?;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<>(),sid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 分页查询交易
     *
     * @param pageNo   当前页
     * @param pageSize 总页数
     * @return
     */
    @Override
    public List<Deal> getPageDeal(Integer pageNo, Integer pageSize) throws SQLException {
        String sql = "select * from ym_deal order by ostartTime desc limit ?,?;";
        List<Deal> dealList = null;
        try{
            dealList = queryRunner.query(DruidUtils.getConnection(),sql, new BeanListHandler<Deal>(Deal.class),(pageNo - 1) * pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return dealList;
    }

    /**
     * 通过商家id查询交易（分页）
     *
     * @param sid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<Deal> getPageDealBySid(Integer sid, Integer pageNo, Integer pageSize) throws SQLException {
        String sql = "select * from ym_deal where sid = ? order by ostartTime desc limit ?,?;";
        List<Deal> dealSidList = null;
            try{
                dealSidList = queryRunner.query(DruidUtils.getConnection(),sql, new BeanListHandler<Deal>(Deal.class),sid,(pageNo - 1) * pageSize,pageSize);
            }catch (Exception e){
                e.printStackTrace();
            }
            return dealSidList;
    }
}
